package cn.com.kblog.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.kblog.common.Constant;
import cn.com.kblog.model.Blog;
import cn.com.kblog.model.BlogType;
import cn.com.kblog.model.Tag;

import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * BlogController
 * @author KK.Luo
 * Email luokai8@vip.qq.com
 * @date 2013-12-4下午2:31:35
 */
@ControllerBind(controllerKey = "/blog",viewPath="WEB-INF/pages")
public class BlogController extends Controller{
	private static final Logger log = Logger.getLogger(BlogController.class);
	
	public void index(){
		_common();
		Map<String, String> params = new HashMap<String, String>();

		//博客类型
		Integer blogtype = getParaToInt("blogtype");
		params.put(Blog.STATUS, Blog.STATUS_1);
		if(blogtype!=null&&!"".equals(blogtype)){
			params.put(Blog.BLOG_TYPE, ""+blogtype);
			setAttr("blog_type", BlogType.dao.findById(blogtype));
		}
		//标签类型
		Integer tag = getParaToInt("tag");
			if(tag!=null&&!"".equals(tag)){
			params.put(Blog.TAG_ID, ""+tag);
			setAttr("tag", Tag.dao.findById(tag));
		}
			
		//页面号
		Integer pageNumber = getParaToInt(0,Constant.PAGE_NUM_DEFAULT);
		//一页条数
		Integer pageSize = getParaToInt("pageSize", Constant.PAGE_SIZE_DEFAULT);
		//排序相关
		String orderField = Blog.UPDATE_TIME;
		String orderDirection = getPara("orderDirection", Constant.PAGE_ORDER_DIRECTION_DEFAULT);
		
		Page<Blog> beans = Blog.dao.pageQuery(pageNumber, pageSize, orderField, orderDirection, params);
		setAttr(Constant.BEANS_KEY, beans);
		render("blog_list.html");
	}

	/**
	 * 阅读博客
	 */
	public void read(){
		_common();
		try {
			Integer blogId =  getParaToInt(0);
			Blog blog = Blog.dao.findById(blogId);
			if(blog==null){
				render( "common/404.html");
				return;
			}
			Blog db_blog = Blog.dao.findById(blogId);
			db_blog.addCount();
			setAttr(Constant.BEAN_KEY, blog);
			setAttr("blog_type",  BlogType.dao.findById(blog.getInt(Blog.BLOG_TYPE)));
			render("blog_read.html");
		} catch (Exception e) {
			log.error(e.getMessage());
		}
	
	}
	
	/**
	 * 搜索BLOG
	 */
	public void search(){
		_common();
		String keywords = getPara("keywords");
		
		if(StrKit.isBlank(keywords)){
			render("index.html");
			return;
		}
		//页面号
		Integer pageNumber = getParaToInt(0,Constant.PAGE_NUM_DEFAULT);
		//一页条数
		Integer pageSize = getParaToInt("pageSize", Constant.PAGE_SIZE_DEFAULT);
		//排序相关
		String orderField = getPara("orderField", Constant.PAGE_ORDER_FIELD_DEFAULT);
		String orderDirection = getPara("orderDirection", Constant.PAGE_ORDER_DIRECTION_DEFAULT);
		
		Page<Blog> beans = Blog.dao.pageQueryByKeywords(pageNumber, pageSize, orderField, orderDirection, keywords);
		setAttr(Constant.BEANS_KEY, beans);
		setAttr("keywords",keywords);
		render("blog_search.html");
	}

	/**
	 * 公共属性设置
	 */
	private void _common(){
		List<Record> tags = Tag.dao.listTop(Constant.TAG_TOP_LIMIT);
		setAttr("top_tags", tags);
		List<BlogType> blogTypes = BlogType.dao.listAll();
		List<Blog> topBlogs = Blog.dao.listTopView();
		setAttr("top_blogs", topBlogs);
		setAttr("blogtypes", blogTypes);
	}
}
